$(function() {
    lay('#version').html('-v' + laydate.v);
    laydate.render({
        elem: '#bg_time'
    });
    laydate.render({
        elem: '#ed_time'
    });
    $('.at_name').on('keyup',function(){
        var num=$(this).val().length;
        $('.changeNum').text(num)
    })
    $(".scs-pt").hide();
    var at_name, b_time, e_time, join_num, id;
    $(".next-pt").on("click", function() {
        at_name = $(".at_name").val();
        b_time = $(".b-time").val();
        e_time = $(".e-time").val();
        join_num = $(".join_num").val();
        if (at_name == "" || b_time == "" || e_time == "" || join_num == "") {
            layer.msg("请完善信息");
        } else if (at_name.length > 45) {
            layer.msg("名称不能超过45个字")
        } else if (join_num < 2 || join_num > 10) {
            layer.msg("请输入2~10人")
        } else if (b_time + ' 00:00:00' >= e_time + ' 23:59:59') {
            layer.msg("结束的时间需大于开始时间")
        } else {
            $(".li1").removeClass("active").addClass("done");
            $(".li2").addClass("active");
            $(".bar").css("width", "100%");
            $("#tab1").removeClass("active");
            $("#tab2").addClass("active");
            $(".next-pt").hide();
        }
    });
    $(".toadd").on("click", function() {
        $(".stepT_1").addClass("step-active");
        $(this).hide();
    });
    $(".stepT").on("click", function(e) {
        var e = e || event;
        var btn_name = e.target.classList[1];
        switch (btn_name) {
            // 搜索框
            case "gd-search":
                var search_cont = $(".search-cont").val();
                $('#searchL').empty();
                ajax("post", "/activity/add-select", { goods_name: search_cont }, function success(data) {
                    if (data.code == 200) {
                        $(".search-list").empty();
                        var searchL = document.getElementById("searchL");
                        var res = data.data.goods_data;
                        for (var i = 0; i < res.length; i++) {
                            var Li = document.createElement("li");
                            var strli = '<li class="li xyb1" data-id="' + res[i].id + '">' + res[i].googs_name + '</li>';
                            Li.innerHTML = strli;
                            searchL.appendChild(Li);
                        }
                    }
                    $(".xyb1").on("click", function() {
                        $(".stepT_2").addClass("step-active").siblings().removeClass("step-active");
                        id = $(this).attr("data-id");
                        var T_body = document.getElementById("sku_tbody");
                        $("#sku_tbody").empty();
                        ajax("post", "/activity/addsku", { id: id },
                            function success(data) {
                                if (data.code == 200) {
                                    console.log(data)
                                    var len = data.data.goods_attr.length;
                                    var is_isset = "false";
                                    var newArr = data.data.goods_attr;
                                    var cdn = data.data.cdn;
                                    for (var i = 0; i < len; i++) {
                                        var str = '<tr><td><input type="checkbox" class="inputc child_ck" attr_value="' + newArr[i].attr_name + ':' + newArr[i].attr_value + '" attr_img="' + newArr[i].attr_img + '" original_price="' + newArr[i].original_price + '" discount_price="' + newArr[i].discount_price + '" inventory="' + newArr[i].inventory + '" goods_attr_id="' + newArr[i].id + '"></td>'
                                        str += '<td>' + newArr[i].attr_value + '</td>'
                                        str += '<td ><img src="' + cdn + newArr[i].attr_img + '"/></td>'
                                        str += '<td>' + newArr[i].original_price + '</td>'
                                        str += '<td>' + newArr[i].discount_price + '</td>'
                                        str += '<td>' + newArr[i].inventory + '</td></tr>'
                                        var Tr = document.createElement("tr");
                                        $('.cnds').val(cdn);
                                        Tr.innerHTML = str;
                                        T_body.appendChild(Tr)
                                    }
                                }
                            }
                        );
                    })
                });
                break;
                // 弹框2的全选
            case "total":
                var status = $(".checkbox").attr("status");
                $(".checkbox").parents(".stepT_2").find("input[type='checkbox']").prop("checked", status == "off" ? true : false);
                $(".checkbox").attr("status", status == "off" ? "on" : "off");
                break;
                // 不是全选
            case "child_ck":
                $(".total").prop("checked", false);
                break;
                // 弹框2的下一步
            case "xyb2":
                $(".stepT_3").addClass("step-active").siblings().removeClass("step-active");
                var is_checked = $("tbody input[type='checkbox']:checked");
                var cndsss = $(".cnds").val();
                var len = is_checked.length;
                var Th_tbody = document.getElementById("Th_tbody");
                $("#Th_tbody").empty();
                for (var i = 0; i < len; i++) {
                    var attr_value = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_value");
                    var attr_img = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_img");
                    var original_price = $("tbody input[type='checkbox']:checked").eq(i).attr("original_price");
                    var discount_price = $("tbody input[type='checkbox']:checked").eq(i).attr("discount_price");
                    var inventory = $("tbody input[type='checkbox']:checked").eq(i).attr("inventory");
                    var goods_attr_id = $("tbody input[type='checkbox']:checked").eq(i).attr("goods_attr_id");

                    var str1 = '<td><input type="text" class="no_width deep" value="' + attr_value + '" disabled></td>'
                    str1 += '<td><input type="hidden" class="no_width deep" value="' + attr_img + '"><img src="' + cndsss + attr_img + '"/></td>'
                    str1 += '<td><input type="text" class="no_width shallow" value="' + original_price + '"></td>'
                    str1 += '<td><input type="text" class="no_width shallow" value="' + original_price + '" placeholder="请输入单购价"></td>'
                    str1 += '<td><input type="text" class="no_width shallow" value="' + discount_price + '"></td>'
                    str1 += '<td><input type="text" class="no_width shallow" value="' + inventory + '"></td>'
                    str1 += '<td style="width:0;"><input type="hidden" value="' + goods_attr_id + '"></td>'

                    var Tr = document.createElement("tr");
                    Tr.innerHTML = str1;
                    Th_tbody.appendChild(Tr)
                }
                break;
                //弹框2的上一步
            case "syb2":
                $(".stepT_1").addClass("step-active").siblings().removeClass("step-active");
                break;
                //弹框3的上一步
            case "syb3":
                $(".stepT_2").addClass("step-active").siblings().removeClass("step-active");
                break;
                //弹框3的确认
            case "qd":
                $(".gd-info").addClass("step-active").siblings().removeClass("step-active");
                $(".scs-pt").css("background", "#87bb33");
                var Qrinfo = document.getElementById("qrinfo");
                $("#qrinfo").empty();
                $(".scs-pt").show();
                $("#Th_tbody tr").each(function(i) {
                    var Tr = document.createElement("tr");
                    var str2 = "";
                    var gd_value = [];
                    $("#Th_tbody tr").eq(i).find("td").each(function(j) {
                        gd_value.push($("#Th_tbody tr").eq(i).find("input").eq(j).val());
                    });
                    var cndsss = $(".cnds").val();
                    str2 += '<td><input class="text" value="' + gd_value[0] + '" disabled="disabled"></td>'
                    str2 += '<td><input class="hidden" value="' + gd_value[1] + '" disabled="disabled"><img src="' + cndsss + gd_value[1] + '"/></td>'
                    str2 += '<td><input class="text" value="' + gd_value[2] + '" disabled="disabled"></td>'
                    str2 += '<td><input class="text" value="' + gd_value[3] + '" disabled="disabled"></td>'
                    str2 += '<td><input class="text" value="' + gd_value[4] + '" disabled="disabled"></td>'
                    str2 += '<td><input class="text" value="' + gd_value[5] + '" disabled="disabled"></td>';
                    str2 += '<td>0</td>';
                    str2 += '<td style="width:0;"><input type="hidden" value="' + gd_value[6] + '" disabled="disabled"></td>'
                    Tr.innerHTML = str2;
                    Qrinfo.appendChild(Tr);
                });

                function jungleF() {
                    var flag = true;
                    $("#Th_tbody tr").each(function(m) {
                        $("#Th_tbody tr").eq(m).find("td").each(function(n) {
                            var jungleI = $("#Th_tbody tr").eq(m).find("input").eq(n).val();
                            if (jungleI == "") {
                                return flag = false;
                            }
                        })
                    })
                }
                console.log(jungleF())
                break;
                //编辑
            case "edit":
                $(".stepT_3").addClass("step-active").siblings().removeClass("step-active");
                $(".gd-info").hide();
                $(".scs-pt").hide();
                break;

            case "qrsyb":
                $("#tab1,.qrxyb").show();
                $(".gd-info").removeClass("step-active")
                $("#tab2").removeClass("active");
                break;

            default:
                break;
        }
    });
    $(".qrxyb").on("click", function() {
        $("#tab1").hide();
        $("#tab2").addClass("active");
        $(".gd-info").show();
        at_name = $(".at_name").val();
        b_time = $(".b-time").val();
        e_time = $(".e-time").val();
        join_num = $(".join_num").val();
    })
    // 完成
    $(".scs-pt").on("click", function() {
        var len = $("#qrinfo tr").length;
        var tArray = new Array();
        for (var i = 0; i < len; i++) {
            var gd_info = [];
            for (var j = 0; j < 7; j++) {
                gd_info.push($("#qrinfo tr").eq(i).find("input").eq(j).val());
            }
            console.log(gd_info)
            tArray.push({ attr_value: gd_info[0], attr_img: gd_info[1], original_price: gd_info[2], one_price: gd_info[3], discount_price: gd_info[4], inventory: gd_info[5], goods_attr_id: gd_info[6] });
        };
        console.log(tArray)
        ajax("post", "/activity/add", { avtivity_attr: tArray, goods_id: id, activity_name: at_name, activity_num: join_num, begin_time: b_time, end_time: e_time },
            function success(data) {
                if (data.code == 200) {
                    layer.msg(data.msg);
                    setTimeout(function() {
                        location.href = "/activity/activity";
                    }, 1000)
                }
            }
        )
    });

    $(".xyb1").on("click", function() {
        $(".stepT_2").addClass("step-active").siblings().removeClass("step-active");
        id = $(this).attr("data-id");
        var T_body = document.getElementById("sku_tbody");
        $("#sku_tbody").empty();
        ajax("post", "/activity/addsku", { id: id },
            function success(data) {
                if (data.code == 200) {
                    console.log(data)
                    var len = data.data.goods_attr.length;
                    var is_isset = "false";
                    var newArr = data.data.goods_attr;
                    var cdn = data.data.cdn;
                    for (var i = 0; i < len; i++) {
                        var str = '<tr><td><input type="checkbox" class="inputc child_ck" attr_value="' + newArr[i].attr_name + ':' + newArr[i].attr_value + '" attr_img="' + newArr[i].attr_img + '" original_price="' + newArr[i].original_price + '" discount_price="' + newArr[i].discount_price + '" inventory="' + newArr[i].inventory + '" goods_attr_id="' + newArr[i].id + '"></td>'
                        str += '<td>' + newArr[i].attr_value + '</td>'
                        str += '<td><img src="' + cdn + newArr[i].attr_img + '"/></td>'
                        str += '<td>' + newArr[i].original_price + '</td>'
                        str += '<td>' + newArr[i].discount_price + '</td>'
                        str += '<td>' + newArr[i].inventory + '</td></tr>'
                        var Tr = document.createElement("tr");
                        $('.cnds').val(cdn);
                        Tr.innerHTML = str;
                        T_body.appendChild(Tr)
                    }
                }
            }
        );
    })
})